//
// Created by PC on 2022/12/27.
//

#include <iostream>

using namespace std;

int main02()
{
    int p = 20, e = 34, i = 325,d = 5;
    for (int j = 1; j <= 21252; ++j)
    {
        if ((j - p) % 23 == 0 and (j - e) % 28 == 0 and (j - i) % 33 == 0)
        {
            printf("%d", j-d);
            return 0;
        }
    }
    return 0;
}


int main(){
    int p,e,i,d;int n=1;
    while(cin>>p>>e>>i>>d&&p!=-1&&e!=-1&&i!=-1&&d!=-1){
        int j;
        p=p%23;
        e=e%28;
        i=i%33;
        for(j=p+23;j>=0;j=j+23){
            if((j-e)%28==0 and (j-i)%33==0){
                    break;
            }
        }
        cout<<"Case "<<n<<": "<<"the next triple peak occurs in "<<j-d<<" days."<<endl;
        n++;
    }
    return 0;
}
